/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-08-28 16:43:18
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-08-31 19:03:07
 * @FilePath: \keep_motion_api\controllers\permission\menu.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Menu } = require('@/models/permission');


const getMenu = async (req, res) => {
  try {
    const { parentId, name, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Menu.getMenu(parentId, name, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const createMenu = async (req, res) => {
  try {
    const { name, path,icon, component, order, parentId } = req.body;
    const menu = await Menu.createMenu({ name, path, icon, component, order, parentId });
    res.status(200).json(menu);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const updateMenu = async (req, res) => {
  try {
    const { id } = req.params;
    const { name, path, icon, component, order, hidden } = req.body;
    const menu = await Menu.updateMenu(id, { name, path, icon, component, order, hidden });
    res.status(200).json({ message: '更新菜单成功', menu });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const deleteMenu = async (req, res) => {
  try {
    const { id } = req.params;
    const menu = await Menu.deleteMenu(id);
    res.status(200).json({ message: '删除菜单成功', menu });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const getPath = async (req, res) => {
  try {
    const { id } = req.params;
    const rows = await Menu.getPath(id);
    res.status(200).send({ rows });
  } catch (error) {
    res.status(500).send({ message: error.message });
  }
}


module.exports = {
  getMenu,
  createMenu,
  updateMenu,
  deleteMenu,
  getPath
}